package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.vo.MarketCategoryValue;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketCategoryValueMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.Date;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月30日 15:14
 */
public class CategoryServiceimpl implements CategoryService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

    @Override
    public List<MarketCategoryValue> selectByMap() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryValueMapper mapper = sqlSession.getMapper(MarketCategoryValueMapper.class);
        List<MarketCategoryValue> marketCategoryValues = mapper.selectByMap();
        return marketCategoryValues;
    }

    @Override
    public List<MarketCategory> selectById(Integer id1) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper mapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id1).andLevelEqualTo("L2").andPidGreaterThan(0);//必须根据pid找到L1的否则就返回null，那么刚刚goods里面的id就是一级id
        List<MarketCategory> marketCategories = mapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public List<MarketCategory> selectAll() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCategoryMapper mapper = sqlSession.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdGreaterThan(0);
        List<MarketCategory> marketCategories = mapper.selectByExample(example);
        return marketCategories;
    }

    //新增的2023年6月1日21:31:30

    @Override
    public List<MarketCategory> getAll() {
        MarketCategoryMapper categoryMapper = sqlSessionFactory.openSession().getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andLevelNotEqualTo("L3");
        return categoryMapper.selectByExample(example);
    }

    @Override
    public BaseRespVo create(MarketCategory category) {
        TransactionUtil.startTransaction();
        BaseRespVo error = validate(category);
        if (error != null) {
            return error;
        }
        this.add(category);
        TransactionUtil.commit();
        return BaseRespVo.ok(category);
    }


    @Override
    public void add(MarketCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        MarketCategoryMapper categoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        categoryMapper.insertSelective(category);


    }

    @Override
    public List<MarketCategory> queryBypid(Integer pid) {
        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andPidEqualTo(pid).andDeletedEqualTo(false);
        MarketCategoryMapper categoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        return categoryMapper.selectByExample(example);
    }

    @Override
    public List<MarketCategory> queryL1() {
        MarketCategoryExample example = new MarketCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        MarketCategoryMapper categoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        return categoryMapper.selectByExample(example);
    }

    @Override
    public int updateById(MarketCategory category) {
        category.setUpdateTime(new Date());
        MarketCategoryMapper categoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public BaseRespVo update(MarketCategory category) {
        TransactionUtil.startTransaction();

        this.updateById(category);
        TransactionUtil.commit();
        return BaseRespVo.ok(category);
    }

    @Override
    public void deleteById(Integer id) {
        MarketCategoryMapper categoryMapper = TransactionUtil.getMapper(MarketCategoryMapper.class);
        categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public BaseRespVo delete(MarketCategory category) {
        TransactionUtil.startTransaction();
        Integer id = category.getId();
        if (id == null) {
            return BaseRespVo.badArgument();
        }
        this.deleteById(category.getId());
        TransactionUtil.commit();
        return BaseRespVo.ok();
    }


    private BaseRespVo validate(MarketCategory category) {
        String name = category.getName();
        if (StringUtil.isEmpty(name)) {
            return BaseRespVo.badArgument();
        }

        String level = category.getLevel();
        if (StringUtil.isEmpty(level)) {
            return BaseRespVo.badArgument();
        }
        if (!level.equals("L1") && !level.equals("L2")) {
            return BaseRespVo.fail(123, "无L1，L2");
        }

        Integer pid = category.getPid();
        if (level.equals("L2") && (pid == null)) {
            return BaseRespVo.badArgument();
        }

        return null;
    }

}
